﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Setups
{
    /// <summary>
    /// 待宰圈设置新增
    /// </summary>
    public class WaitingCreateHandler : IRequestHandler<WaitingCreateCommand, Result>
    {

        private readonly IBaseRepository<Waitings> waitingsRepository;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="waitingsRepository"></param>
        /// <param name="mapper"></param>
        public WaitingCreateHandler(IBaseRepository<Waitings> waitingsRepository, IMapper mapper)
        {
            this.waitingsRepository = waitingsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(WaitingCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };

            try
            {
                Waitings waitings = mapper.Map<Waitings>(request);
                var list = waitingsRepository.Add(waitings);

                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;

                }
                else
                {
                    result.Msg = "失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
